package cn.com.w.mongodb.util;

import java.io.*;

public class JsonParseUtil {


    /**
     * 把一个文件中的内容读取成一个String字符串
     *
     * @param jsonFile
     * @return
     */
    private static String getStr(File jsonFile){
        String jsonStr = "";
        try {
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取json字符串
     *
     * @param path
     * @return
     */
    public static String getJsonStr(String path){
        File jsonFile = new File(path);
        if (!jsonFile.exists()) {
            return null;
        }
        return getStr(jsonFile);
    }





}
